﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.Part
{
    public partial class PPARModifyy : PageBase
    {
        PPARBO bo = new PPARBO();
        PPARBO ppbo = new PPARBO();
        UsersBO ubo = new UsersBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = null;
                if (CurrentUser.UnitId == "0")
                    ds = ppbo.GetPlantListForPlantID(CurrentUser.UnitName);
                else
                    ds = ppbo.GetPlantListForSupplierid(CurrentUser.UnitId == "99" ? CurrentUser.Id : null);
                ddlPlant.Items.Add(new ListItem("All", "All"));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ddlPlant.Items.Add(new ListItem(dr["plantname"].ToString(), dr["id"].ToString()));
                    }
                }
                else
                {
                    ddlPlant.Items.Add(new ListItem("Not added plant information", "0"));
                }

                    ddlStatus.Items.Add(new ListItem("All", "All"));
                    ddlStatus.Items.Add(new ListItem("Un-allocated", "-1"));
                    ddlStatus.Items.Add(new ListItem("Allocated", "0"));
                    ddlStatus.Items.Add(new ListItem("Created", "1"));
                    ddlStatus.Items.Add(new ListItem("Submitted", "2"));
                    ddlStatus.Items.Add(new ListItem("Ready for sampling", "3"));
                    ddlStatus.Items.Add(new ListItem("Not ready for sampling", "4"));
                    ddlStatus.Items.Add(new ListItem("Circulating", "5"));
                    ddlStatus.Items.Add(new ListItem("Released", "6"));
                    ddlStatus.Items.Add(new ListItem("Decided", "7"));
                btnSubmit_Click(null, null);
            }
        }

        //状态显示

        public string ShowAuditStatus(string Status, string FINALSTATUS, string isinapp)
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    if (isinapp == "0")
                        result = "<span style='color:red'>Un-allocated</span>";
                    else
                        result = "<span style='color:blue'>Allocated</span>";
                    break;
                case "1":
                    result = "<span style='color:red'>Created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>Submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>Not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Circulating</span>";
                    break;
                case "6":
                    if (FINALSTATUS == "0")
                        result = "<span style='color:red'>Circulating</span>";
                    else
                        result = "<span style='color:blue'>Released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>Decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            return result;
        }

        /// <summary>
        /// 根据状态显示修改状态
        /// </summary>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool ShowEditButton(string Status)
        {
            switch (Status.ToLower())
            {
                case "0":
                case "1":
                case "4":
                    return true;
                default:
                    return false;
            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = GetDataSet("");// bo.GetListForWhere(txtPartno.Text, ddlPlant.SelectedItem.Text, cbAll.Checked, CurrentUser.UnitId == "0" ? CurrentUser.UnitName : null);
                AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
                dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                dgList.DataBind();
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
        }

        private DataSet GetDataSet(string sort)
        {
            string sqlwhere = "";
            if (!cbAll.Checked)
            {
                if (txtPartno.Text.Trim() != "")
                    sqlwhere += "   and lower(bdntpartno) like lower('%" + txtPartno.Text.Trim() + "%') ";
                if (ddlPlant.SelectedValue != "All")
                    sqlwhere += "   and lower(p.plantname) like lower('%" + ddlPlant.SelectedItem.Text + "%') ";
                if (txtPartName.Text.Trim() != "")
                    sqlwhere += " and lower(bdntpartname) like lower('%" + txtPartName.Text.Trim() + "%') ";
                if (ddlStatus.SelectedValue != "All")
                {
                    if ((CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99") && ddlStatus.SelectedValue == "3") //供应商
                        sqlwhere += " and (a.status=3 or a.status=5 or a.status=6) ";
                    else
                    {
                        if (ddlStatus.SelectedValue == "5")
                            sqlwhere += "  and( a.status=5 or (a.status=6 and a.FINALSTATUS=0)) ";
                        else if (ddlStatus.SelectedValue == "6")
                            sqlwhere += " and a.status=6 and  a.FINALSTATUS!=0 ";
                        else if (ddlStatus.SelectedValue == "-1")
                            sqlwhere += " and a.status=0 and a.isinapp=0 ";
                        else if (ddlStatus.SelectedValue == "0")
                            sqlwhere += " and a.status=0 and a.isinapp=1 ";
                        else
                            sqlwhere += " and a.status=" + ddlStatus.SelectedValue;
                    }

                }
                if (tbVersion.Text.Trim() != "")
                    sqlwhere += " and lower(bdntrevisionno) like lower('%" + tbVersion.Text.Trim() + "%') ";
                if (DatePicker1.Text.Trim() != "")
                    sqlwhere += " and trunc(a.ppardate)>=to_date( '" + DatePicker1.Text.Trim() + "', 'yyyy-mm-dd ') ";
                if (DatePicker2.Text.Trim() != "")
                    sqlwhere += " and trunc(a.ppardate)<=to_date( '" + DatePicker2.Text.Trim() + "', 'yyyy-mm-dd ') ";
                if (txtColor.Text.Trim() != "")
                    sqlwhere += " and lower(r.color) like lower('%" + txtColor.Text.Trim() + "%') ";
                if (ddlResult.SelectedValue != "")
                    sqlwhere += " and lower(FINALRESULT)=lower('" + ddlResult.SelectedValue.Trim() + "') and a.status=7 ";
                if (txtSuppliername.Text.Trim() != "")
                    sqlwhere += " and lower(s.suppliername) like lower('%" + txtSuppliername.Text.Trim() + "%') ";
                if (txtSuppliercode.Text.Trim() != "")
                    sqlwhere += " and lower(s.suppliercode) like lower('%" + txtSuppliercode.Text.Trim() + "%') ";
                if (txtPPARNO.Text.Trim() != "")
                    sqlwhere += " and lower('PPAR-'||a.pparno||'.'||PPARVERSION) like lower('%" + txtPPARNO.Text.Trim() + "%') ";
            }
            sqlwhere += " and a.status!=9 ";
            if (!ubo.IsAdmin(CurrentUser.Id)) //如果是管理员，可以查看修改所有part date
            {
                sqlwhere += "  and( a.qeuser='"+CurrentUser.Id+"' or u.substitutesuser='"+CurrentUser.Id+"') ";
            }
            sqlwhere += sort;
            DataSet ds = bo.GetListForWhere4(sqlwhere);
            return ds;
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            BindGridView();
        }


        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void dgList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.dgList.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.dgList.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.dgList.Attributes["SortExpression"] = sortExpression;
            this.dgList.Attributes["SortDirection"] = sortDirection;


            string pic = "";
            pic = sortDirection == "ASC" ? "<img src='../Images/icon/arrowup.gif' />" : "<img src='../Images/icon/arrowdown.gif' />";

            foreach (DataGridColumn item in dgList.Columns)
            {
                if (item.SortExpression == sortExpression)
                {
                    item.HeaderText = item.FooterText + " " + pic;
                }
                else
                    item.HeaderText = item.FooterText;
            }

            this.BindGridView();

        }

        private void BindGridView()
        {

            // 获取GridView排序数据列及排序方向
            string sortExpression = this.dgList.Attributes["SortExpression"];
            string sortDirection = this.dgList.Attributes["SortDirection"];
            if (sortExpression != null)
            {
                // 调用业务数据获取方法
                string sort = string.Format("  order by lower({0})  {1} ", sortExpression, sortDirection);
                if (sortExpression == "ppardate" || sortExpression == "endtime"||sortExpression=="planedppardate")
                {
                    sort = string.Format("  order by {0}  {1} ", sortExpression, sortDirection);
                }
                DataTable dtBind = GetDataSet(sort).Tables[0];
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }
                // GridView绑定并显示数据
                this.dgList.DataSource = GetPageTable(dtBind, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                this.dgList.DataBind();
            }
            else
                btnSubmit_Click(null, null);
        }
    }
}